//
//  ViewModelClass.m
//  ReactiveCocoa
//
//  Created by 李松 on 2017/12/2.
//  Copyright © 2017年 lisong. All rights reserved.
//

#import "ViewModelClass.h"
#import "NetRequestClass.h"
#import "WeiboItem.h"

@implementation ViewModelClass

- (void)setBlockWithSucceedBlock:(SucceedBlock)succeedBlock withErrorBlock:(ErrorBlock)errorBlock{
    
    _succeedBlock = succeedBlock;
    _errorBlock = errorBlock;
}

- (void)getWeiBoList{
    
    NSDictionary *parameter = @{TOKEN: ACCESSTOKEN,
                                COUNT: @"100"
                                };
    [NetRequestClass NetRequestGetWithRequestURLString:REQUESTPUBLICURL WithParameter:parameter WithSucceedBlock:^(id succeedValue) {
        
        [self getValueSuccessWithDic:succeedValue];
    } WithErrorBlock:^(id errorValue) {
        [self errorCodeWitherror:errorValue];
    }];
}

-(void)getValueSuccessWithDic: (NSDictionary *) returnValue{
    
    NSArray *statuses = returnValue[STATUSES];
    NSMutableArray *publicModelArray = [[NSMutableArray alloc] initWithCapacity:statuses.count];
    
    for (int i = 0; i < statuses.count; i ++) {
        WeiboItem *item = [[WeiboItem alloc] init];
        
        //设置时间
        NSDateFormatter *iosDateFormater=[[NSDateFormatter alloc]init];
        iosDateFormater.dateFormat=@"EEE MMM d HH:mm:ss Z yyyy";
        
        //必须设置，否则无法解析
        iosDateFormater.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
        NSDate *date=[iosDateFormater dateFromString:statuses[i][CREATETIME]];
        
        //目的格式
        NSDateFormatter *resultFormatter=[[NSDateFormatter alloc]init];
        [resultFormatter setDateFormat:@"MM月dd日 HH:mm"];
        
        item.date = [resultFormatter stringFromDate:date];
        item.userName = statuses[i][USER][USERNAME];
        item.text = statuses[i][WEIBOTEXT];
        item.imageUrl = [NSURL URLWithString:statuses[i][USER][HEADIMAGEURL]];
        item.userId = statuses[i][USER][UID];
        item.weiboId = statuses[i][WEIBOID];
        
        [publicModelArray addObject:item];
        
    }
    
    self.succeedBlock(publicModelArray);
    
}

-(void) errorCodeWitherror: (NSString *) error
{
    self.errorBlock(error);
}

- (void)ls_racGetList{
    
    self.requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
       
        RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            
            NSDictionary *parameter = @{TOKEN: ACCESSTOKEN,
                                        COUNT: @"100"
                                        };
            [NetRequestClass NetRequestGetWithRequestURLString:REQUESTPUBLICURL WithParameter:parameter WithSucceedBlock:^(id succeedValue) {
                
                [subscriber sendNext:succeedValue];
                [subscriber sendCompleted];
            } WithErrorBlock:^(id errorValue) {
                [self errorCodeWitherror:errorValue];
            }];
            
            return nil;
        }];
        
        return signal;
    }];
}

@end
